<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/8/20
 * Time: 11:10
 */
namespace app\driver\controller;
use think\Db;
class Order extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }

    //我的运单
    public function index(){
        extract(input());
        $type = input('param.type');// 1进行中  2代付款 3待评价  4取消订单
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['a.order_dirver_user'] = $user_id;
        if ($type==1){//进行中
            $where['a.order_type'] = array('not in',array(3,4));
            $where['a.order_status'] = array('IN',array(5,6));
            $where['a.order_true'] =1;
            $res = Db::table('up_order')->alias('a')
                ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi')
                ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
                ->limit($limit,$num)
                ->where($where)
                ->order('a.order_fb_time desc')
                ->field('a.order_num,ucz.zxlx_name,a.order_goods_m,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi,uuu.user_image,uuu.user_phone,uuu.user_username,uuu.user_real_name')
                ->select();
            return json(['status' => 200, 'msg' => $res]);
        }elseif ($type==2){//待付款
            $where['a.order_type'] = array('not in',array(3,4));
            $where['a.order_status'] = 7;
            $res = Db::table('up_order')->alias('a')
                ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi')
                ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
                ->limit($limit,$num)
                ->where($where)
                ->order('a.order_fb_time desc')
                ->field('a.order_num,ucz.zxlx_name,a.order_goods_m,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi,uuu.user_image,uuu.user_phone,uuu.user_username,uuu.user_real_name')
                ->select();
            return json(['status' => 200, 'msg' => $res]);
        }elseif ($type==3){//待评价
            $where['a.order_type'] = array('not in',array(3,4));
            $where['a.order_status'] = 8;
            $where['a.sj_pj'] = 1;
            $res = Db::table('up_order')->alias('a')
                ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi')
                ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
                ->limit($limit,$num)
                ->where($where)
                ->order('a.order_fb_time desc')
                ->field('a.order_num,a.sj_pj,ucz.zxlx_name,a.order_goods_m,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi,uuu.user_image,uuu.user_phone,uuu.user_username,uuu.user_real_name')
                ->select();
            return json(['status' => 200, 'msg' => $res]);
        } elseif ($type==5){//全部订单
            $where['a.order_type'] = array('not in',array(3,4));
            $where['a.order_status'] = array('>',2);
            $res = Db::table('up_order')->alias('a')
                ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->join('up_u_user uuu','uuu.user_id = a.order_jms_user','left')
                ->limit($limit,$num)
                ->where($where)
                ->order('a.order_fb_time desc')
                ->field('a.order_num,ucz.zxlx_name,a.order_goods_m,a.order_type,a.order_status,a.order_fb_time,
                         a.cx_id as cx_name,uch.hwlx_name,a.sj_pj,a.jms_pj,
                         a.order_cc,a.order_goods_ton,a.order_zc_time,
                         a.order_id,ua.district as go_sheng,ua1.district as go_shi,
                         ua2.district as to_sheng,ua3.district as to_shi,uuu.user_image,
                         uuu.user_phone,uuu.user_username,uuu.user_real_name')
                ->select();
            return json(['status' => 200, 'msg' => $res]);
        }elseif ($type==6){//待确认
            $where['a.order_type'] = array('not in',array(3,4));
            $where['a.order_status'] = 4;
            $res = Db::table('up_order')->alias('a')
                ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->join('up_u_user uuu','uuu.user_id = a.order_jms_user','left')
                ->limit($limit,$num)
                ->where($where)
                ->order('a.order_fb_time desc')
                ->field('a.order_num,ucz.zxlx_name,a.order_goods_m,a.order_type,a.order_status,a.order_fb_time,
                         a.cx_id as cx_name,uch.hwlx_name,
                         a.order_cc,a.order_goods_ton,a.order_zc_time,
                         a.order_id,ua.district as go_sheng,ua1.district as go_shi,
                         ua2.district as to_sheng,ua3.district as to_shi,uuu.user_image,
                         uuu.user_phone,uuu.user_username,uuu.user_real_name')
                ->select();
            return json(['status' => 200, 'msg' => $res]);
        }elseif($type==7){//已完成
            $where['a.order_type'] = array('not in',array(3,4));
            $where['a.order_status'] = 9;
            $res = Db::table('up_order')->alias('a')
                ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->join('up_u_user uuu','uuu.user_id = a.order_jms_user','left')
                ->limit($limit,$num)
                ->where($where)
                ->order('a.order_fb_time desc')
                ->field('a.order_num,ucz.zxlx_name,a.order_goods_m,a.order_type,a.order_status,a.order_fb_time,
                         a.cx_id as cx_name,uch.hwlx_name,
                         a.order_cc,a.order_goods_ton,a.order_zc_time,
                         a.order_id,ua.district as go_sheng,ua1.district as go_shi,
                         ua2.district as to_sheng,ua3.district as to_shi,uuu.user_image,
                         uuu.user_phone,uuu.user_username,uuu.user_real_name')
                ->select();
            return json(['status' => 200, 'msg' => $res]);
        }//elseif ($type==4){//取消订单
//            $orderIdList = Db::table('up_order_cancel')->distinct(true)->where('user_id',$user_id)->column('order_id');
//            $where['a.order_id'] = array('IN',$orderIdList);
//            $res = Db::table('up_order')->alias('a')
//                ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
//                ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
//                ->join('up_area ua','ua.district_id = a.order_go_sheng')
//                ->join('up_area ua1','ua1.district_id = a.order_go_shi')
//                ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
//                ->join('up_area ua3','ua3.district_id = a.order_to_shi')
//                ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
//                ->limit($limit,$num)
//                ->where($where)
//                ->order('a.order_fb_time desc')
//                ->field('ucz.zxlx_name,a.order_goods_m,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
//            a.order_cc,a.order_goods_ton,a.order_zc_time,
//            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
//            ua2.district as to_sheng,ua3.district as to_shi,uuu.user_image,uuu.user_phone,uuu.user_username,uuu.user_real_name')
//                ->select();
//            return json(['status' => 200, 'msg' => $res]);
//        }
    }
    public function indexNum(){
        extract(input());
        //进行中
        $where['a.order_dirver_user'] = $user_id;
        $where['a.order_type'] = array('not in',array(3,4));
        $where['a.order_status'] = array('IN',array(5,6));
        $where['a.order_true'] =1;
        $num = Db::table('up_order')->alias('a')
            ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
            ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi')
            ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
            ->where($where)
            ->count();
        //待确认
        $where1['a.order_type'] = array('not in',array(3,4));
        $where1['a.order_status'] = 4;
        $where1['a.order_dirver_user'] = $user_id;
        $num1= Db::table('up_order')->alias('a')
            ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
            ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
            ->join('up_u_user uuu','uuu.user_id = a.order_jms_user','left')
            ->where($where1)
            ->count();
        //待评价
        $where2['a.order_type'] = array('not in',array(3,4));
        $where2['a.order_status'] = 8;
        $where2['a.sj_pj'] = 1;
        $where2['a.order_dirver_user'] = $user_id;
        $num2 = Db::table('up_order')->alias('a')
            ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
            ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi')
            ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
            ->where($where2)
            ->count();
        //待付款
        $where3['a.order_type'] = array('not in',array(3,4));
        $where3['a.order_status'] = 7;
        $where3['a.order_dirver_user'] = $user_id;
        $num3 = Db::table('up_order')->alias('a')
            ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
            ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi')
            ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
            ->where($where3)
            ->count();
        $res['jxz'] = $num;
        $res['dqr'] = $num1;
        $res['dpj'] = $num2;
        $res['dfk'] = $num3;
        return json(['status' => 200, 'msg' => $res]);
    }
      //电子协议回显
     public function huixian(){
         extract(input());
         $order_id = input('param.order_id');
         $res = Db::table('up_order')->where('order_id',$order_id)->field('order_dzxy_freight,order_id,order_dzxy_yf')->find();
         return json(['status'=>200,'msg'=>$res]);
     }
     //发送电子协议
     public function dianzi(){
         extract(input());
         $order_id = input('param.order_id');
         $order_dzxy_freight = input('param.order_dzxy_freight');
         $data['order_dzxy_freight'] = $order_dzxy_freight;
         $data['order_status'] = 4;
         $res = Db::table('up_order')->where('order_id',$order_id)->update($data);
         if($res){
             //查询这个订单的加盟商
             $order_jms_user = Db::table('up_order')->where('order_id',$order_id)->field('order_dirver_user,order_jms_user')->find();
             //查询这个加盟商的手机号码
             $user_phone = Db::table('up_u_user')->where('user_id',$order_jms_user['order_jms_user'])->value('user_phone');
             //查询这个司机的昵称
             $user_username = Db::table('up_u_user')->where('user_id',$user_id)->value('user_username');
             $dataList['n_title'] = '您的订单已被司机'.$user_username.'接单且已发送电子协议，请尽快查看';
             $dataList['n_type'] =  2;
             $dataList['n_notice_type'] = 2;
             $dataList['n_user_id'] = ','.$order_jms_user['order_jms_user'].',';
             $dataList['n_read'] = ',0,';
             $dataList['n_delete'] = ',0,';
             $dataList['n_time'] = date('Y-m-d H:i:s');
             $dataList['n_status'] = 2;
             Db::table('up_notice')->insertGetId($dataList);
             YzxSms($user_username, $user_phone, '',562123);//发送验证码

             //推送
             $list1 = [];
             $v = (string)$order_jms_user['order_jms_user'];
//             array_push($list1, $v);
             $code=2;
             $str = array('msg'=>'您的订单已被司机'.$user_username.'接单且已发送电子协议，请尽快查看','type'=>9,'order_id'=>$order_id);
             $str1 = '您的订单已被司机'.$user_username.'接单且已发送电子协议，请尽快查看';
             try{
                 $this->Jpush1(1, $v, $str1, $str,$code);
             }catch(\Exception $e){

             }
             return json(['status'=>200,'msg'=>"提交成功"]);
         }else{
             return json(['status'=>100,'msg'=>"提交失败"]);
         }
     }
     //取消订单
     public function quxiao(){
         extract(input());
         $order_id = input('param.order_id');
         $info =Db::table('up_order')->where('order_id',$order_id)->find();
         if($info['order_status']>=6){
             return json(['status' => 100, 'msg' => "订单已经装箱,不能取消订单"]);
         }else{
             $data['order_dirver_user'] = '';
             $data['order_zc_goods_img'] = '';
             $data['order_xh_goods_img'] = '';
             $data['order_status'] = 2;
             $data['order_dzxy_freight'] = '';
             $data['order_dzxy_yf'] = '';
             if($info['order_type']==1){
                 $data['order_true'] = 3;
             }

             $res =Db::table('up_order')->where('order_id',$order_id)->update($data);
             if($res){
                 $district = Db::table('up_area')->where('district_id',$info['order_go_sheng'])->value('district');
                 $district1 = Db::table('up_area')->where('district_id',$info['order_to_sheng'])->value('district');
                 //插入消息表
                 $dataList['n_title'] = "您的".$district.'--'.$district1."的订单已取消";
                 $dataList['n_type'] =  2;
                 $dataList['n_notice_type'] = 2;
                 $dataList['n_user_id'] = ','.$info['order_jms_user'].','.$info['order_qy_user'].',';
                 $dataList['n_read'] = ',0,';
                 $dataList['n_delete'] = ',0,';
                 $dataList['n_time'] = date('Y-m-d H:i:s');
                 $dataList['n_status'] = 2;
                 Db::table('up_notice')->insertGetId($dataList);
//                 $userList = array_merge($info['order_qy_user'],$info['order_jms_user']);
                 //推送

                 $code=2;
                 $str = array('msg'=>"您的".$district.'--'.$district1."的订单已取消",'type'=>2,'order_id'=>$order_id);
                 $str1 = "您的".$district.'-'.$district1."的订单已取消";
                 $list = $list1 = [];
//                 array_push($list, $info['order_jms_user']);
//                 array_push($list1, $info['order_qy_user']);
                 $v = (string)$info['order_jms_user'];
                 $v1 = (string)$info['order_qy_user'];
                 //推送加盟商
                 try{
                     $this->Jpush1(1, $v, $str1, $str,$code);
                 }catch(\Exception $e){

                 }
                 //推送企业
                 try{
                     $this->Jpush2(1, $v1, $str1, $str,$code);
                 }catch(\Exception $e){

                 }
                 //插入取消订单表
                 $data1['user_id'] = $user_id;
                 $data1['order_id'] = $order_id;
                 $data1['cancel_time'] = date('Y-m-d H:i:s');
                 Db::table('up_order_cancel')->insertGetId($data1);
                 return json(['status'=>200,'msg'=>"取消订单成功"]);
             }else{
                 return json(['status'=>100,'msg'=>"取消订单失败"]);
             }
         }
     }

    //装车拍照
    public function zcpz(){
        extract(input());
        $order_id = input('param.order_id');
        $order_zc_goods_img = input('param.order_zc_goods_img');
        $order_zc_goods_video = input('param.order_zc_goods_video');
        $data['order_zc_goods_img'] = $order_zc_goods_img;
        $data['order_zc_goods_video'] = $order_zc_goods_video;
        $data['order_status'] = 6;
        $data['order_zcsj_time'] = date('Y-m-d H:i:s');
        $res = Db::table('up_order')->where('order_id',$order_id)->update($data);
        if($res){
            $info =Db::table('up_order')->where('order_id',$order_id)->find();
            $district = Db::table('up_area')->where('district_id',$info['order_go_sheng'])->value('district');
            $district1 = Db::table('up_area')->where('district_id',$info['order_to_sheng'])->value('district');
            //插入消息表
            $dataList['n_title'] = "您的".$district.'--'.$district1."的订单已装车拍照";
            $dataList['n_type'] =  2;
            $dataList['n_notice_type'] = 2;
            $dataList['n_user_id'] = ','.$info['order_jms_user'].','.$info['order_qy_user'].',';
            $dataList['n_read'] = ',0,';
            $dataList['n_delete'] = ',0,';
            $dataList['n_time'] = date('Y-m-d H:i:s');
            $dataList['n_status'] = 2;
            Db::table('up_notice')->insertGetId($dataList);
            $code=2;
            $str = array('msg'=>"您的".$district.'--'.$district1."的订单已装车拍照",'type'=>2,'order_id'=>$order_id);
            $str1 = "您的".$district.'-'.$district1."的订单已装车拍照";
            $list = $list1 = [];
//            array_push($list, $info['order_jms_user']);
//            array_push($list1, $info['order_qy_user']);
            $v = (string)$info['order_jms_user'];
            $v1 = (string)$info['order_qy_user'];

            //推送加盟商
            try{
                $this->Jpush1(1, $v, $str1, $str,$code);
            }catch(\Exception $e){

            }
            //推送企业
            try{
                $this->Jpush2(1, $v1, $str1, $str,$code);
            }catch(\Exception $e){

            }
            return json(['status'=>200,'msg'=>"操作成功"]);
        }else{
            return json(['status'=>100,'msg'=>"操作失败"]);
        }
    }
    //投诉
    public function complaint(){
        extract(input());
        $order_id = input('param.order_id');
        $content = input('param.content');
        $data['content'] = $content;
        $data['user_id'] = $user_id;
        $data['order_id'] = $order_id;
        $data['complaint_time'] = date('Y-m-d H:i:s');
        $res = Db::table('up_order_complaint')->where('order_id',$order_id)->insertGetId($data);
        if($res){
            return json(['status'=>200,'msg'=>"操作成功"]);
        }else{
            return json(['status'=>100,'msg'=>"操作失败"]);
        }
    }
    //问题反馈
    public function problem(){
        extract(input());
        $order_id = input('param.order_id');
        $content = input('param.content');
        $img = input('param.img');
        $data['order_id'] = $order_id;
        $data['content'] = $content;
        $data['user_id'] = $user_id;
        $data['goods_problem_img'] = $img;
        $data['good_problem_time'] = date('Y-m-d H:i:s');
        $res = Db::table('up_order_goods_problem')->where('order_id',$order_id)->insertGetId($data);
        if($res){
            return json(['status'=>200,'msg'=>"操作成功"]);
        }else{
            return json(['status'=>100,'msg'=>"操作失败"]);
        }
    }

    //卸车拍照
    public function xcpz(){
        extract(input());
        $order_id = input('param.order_id');
        $order_xh_goods_img = input('param.order_xh_goods_img');
        $order_xh_goods_video = input('param.order_xh_goods_video');
        $data['order_xh_goods_img'] = $order_xh_goods_img;
        $data['order_xh_goods_video'] = $order_xh_goods_video;
        $data['order_status'] = 7;
        $data['order_xhsj_time'] = date('Y-m-d H:i:s');
        $res = Db::table('up_order')->where('order_id',$order_id)->update($data);
        if($res){
            $info =Db::table('up_order')->where('order_id',$order_id)->find();
            $district = Db::table('up_area')->where('district_id',$info['order_go_sheng'])->value('district');
            $district1 = Db::table('up_area')->where('district_id',$info['order_to_sheng'])->value('district');
            //插入消息表
            $dataList['n_title'] = "您的".$district.'--'.$district1."的订单已卸车拍照";
            $dataList['n_type'] =  2;
            $dataList['n_notice_type'] = 2;
            $dataList['n_user_id'] = ','.$info['order_jms_user'].','.$info['order_qy_user'].',';
            $dataList['n_read'] = ',0,';
            $dataList['n_delete'] = ',0,';
            $dataList['n_time'] = date('Y-m-d H:i:s');
            $dataList['n_status'] = 2;
            Db::table('up_notice')->insertGetId($dataList);
            $code=2;
            $str = array('msg'=>"您的".$district.'--'.$district1."的订单已卸车拍照",'type'=>2,'order_id'=>$order_id);
            $str1 = "您的".$district.'-'.$district1."的订单已卸车拍照";
            $list = $list1 = [];
//            array_push($list, $info['order_jms_user']);
//            array_push($list1, $info['order_qy_user']);
            $v = (string)$info['order_jms_user'];
            $v1 = (string)$info['order_qy_user'];
            //推送加盟商
            try{
                $this->Jpush1(1, $v, $str1, $str,$code);
            }catch(\Exception $e){

            }
            //推送企业
            try{
                $this->Jpush2(1, $v1, $str1, $str,$code);
            }catch(\Exception $e){

            }
            return json(['status'=>200,'msg'=>"操作成功"]);
        }else{
            return json(['status'=>100,'msg'=>"操作失败"]);
        }
    }

    public function detailEvaluate(){
        extract(input());
        $where['a.order_id'] = $order_id;
        $res = Db::table('up_order')->alias('a')
            ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi')
            ->where($where)
            ->field('a.order_jms_user,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi')
            ->find();
        return json(['status' => 200, 'msg' => $res]);
    }

    //立即评价
    public function ljEvaluate(){
        extract(input());
        $evaluate_bq = input('param.evaluate_bq');
        $evaluate_star = input('param.evaluate_star');
        $evaluate_review = input('param.evaluate_review');
        $evaluate_content = input('param.evaluate_content');
        $evaluate_b_user_id = input('param.evaluate_b_user_id');
        $evaluate_img = input('param.evaluate_img');
        $order_id = input('param.order_id');

        $data['evaluate_p_user_id'] = $user_id;
        $data['evaluate_b_user_id'] = $evaluate_b_user_id;
        $data['order_id'] = $order_id;
        $data['evaluate_type'] = 1;
        $data['evaluate_content'] = $evaluate_content;
        $data['evaluate_star'] = $evaluate_star;
        $data['evaluate_bq'] = $evaluate_bq;
        $data['evaluate_review'] = $evaluate_review;
        $data['evaluate_img'] = $evaluate_img;
        $data['evaluate_time'] = date('Y-m-d H:i:s');
        $res = Db::table('up_order_evaluate')->insertGetId($data);
        //查询加盟商是否对这个订单是否评价
        $result = Db::table('up_order')->where('order_id',$order_id)->field('order_id,sj_pj')->find();
        if($result['sj_pj']==1){
            Db::table('up_order')->where('order_id',$order_id)->setField('sj_pj',2);
        }
        //查询这个加盟商和司机是否都评价完成
        $result1 = Db::table('up_order')->where('order_id',$order_id)->field('order_status,order_id,jms_pj,sj_pj')->find();
        if($result1['order_status']==8){//待评价
            if($result1['jms_pj']==2&&$result1['sj_pj']==2){//司机和加盟商都评价过
                Db::table('up_order')->where('order_id',$order_id)->setField('order_status',9);
            }
        }
        if($res){
            return json(['status'=>200,'msg'=>"成功"]);
        }else{
            return json(['status'=>100,'msg'=>"失败"]);
        }
    }

    //余额支付
    public function pay(){
        extract(input());
        $order_id = input('param.order_id');
        $money = input('param.money');
        $password = input('param.password');
        $userInfo = Db::table('up_u_user')->where('user_id',$user_id)->field('user_zf_password,user_balance')->find();
        if($password==''){
            return json(['status'=>100,'msg'=>"支付密码不能为空"]);
        }
        if($password!=$userInfo['user_zf_password']){
            return json(['status'=>100,'msg'=>"支付密码输入错误"]);
        }
        if($money>$userInfo['user_balance']){
            return json(['status'=>100,'msg'=>"余额不足,无法完成支付"]);
        }
        $res1 = Db::table('up_order')->where('order_id',$order_id)->find();
        if($res1['order_dirver_user']!=''){
            return json(['status'=>0,'msg'=>'你来迟一步,订单已被接了']);
        }
        Db::startTrans();
        try{
            $info1 = Db::table('up_order')->where('order_id',$order_id)->find();
            if($info1['order_status'] == 2){
                //更新订单状态
                $dataOrder['order_dzxy_yf'] = $money;
                $dataOrder['order_status'] = 3;
                $dataOrder['order_dirver_user'] =$user_id;
                Db::table('up_order')->where('order_id',$order_id)->update($dataOrder);
                $info = Db::table('up_order')->where('order_id',$order_id)->find();
                //插入司机钱包明细
                $order_number = date('YmdHis') . str_pad(mt_rand(1, 99999999), 6, '0', STR_PAD_LEFT).$info['order_dirver_user'];
                $user_real_name = Db::table('up_u_user')
                    ->where('user_id',$info['order_jms_user'])
                    ->value("user_real_name");
                $data1['user_id'] = $info['order_dirver_user'];
                $data1['balance_type'] = 3;
                $data1['balance_time'] = date('Y-m-d H:i:s');
                $data1['balance_title'] = "支付加盟商".($user_real_name).'定金';
                $data1['balance_num'] = $order_number;
                $data1['balance_price'] = $info['order_dzxy_yf'];
                $data1['balance_status'] = 2;
                $data1['balance_jia_jian'] = 2;
                $data1['balance_xs'] = 2;

                $data1['order_paytime'] = date('Y-m-d H:i:s');
                Db::table('up_user_balance')->insertGetId($data1);
                //插入加盟商钱包明细
                $order_number1 = date('YmdHis') . str_pad(mt_rand(1, 99999999), 6, '0', STR_PAD_LEFT).$info['order_jms_user'];
                $user_real_name1 = Db::table('up_u_user')
                    ->where('user_id',$info['order_dirver_user'])
                    ->value("user_real_name");
                $data2['user_id'] = $info['order_jms_user'];
                $data2['balance_type'] = 3;
                $data2['balance_time'] = date('Y-m-d H:i:s');
                $data2['balance_title'] = "收到司机".($user_real_name1).'定金';
                $data2['balance_num'] = $order_number1;
                $data2['balance_price'] = $info['order_dzxy_yf'];
                $data2['balance_status'] = 2;
                $data2['balance_jia_jian'] = 1;
                $data2['balance_xs'] = 1;
                $data2['order_paytime'] = date('Y-m-d H:i:s');
                Db::table('up_user_balance')->insertGetId($data2);
                //扣除司机账户余额
                Db::table('up_u_user')->where('user_id',$user_id)->setDec('user_balance',$money);
                Db::table('up_u_user')->where('user_id',$info['order_jms_user'])->setInc('user_balance',$money);
            }
            Db::commit();
            return json(['status'=>200,'msg'=>"支付成功"]);
        }catch (\Exception $e) {
            Db::rollback();
            return json(['status'=>100,'msg'=>"支付失败"]);
        }
    }

    /**
     * 用户服务协议
     * @return \think\response\Json
     */
    public function agreement(){
        $agreement = Db::table('up_other')->where('other_id', 11)->value('other_price');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }

    //地图打点
    public function dot(){
        extract(input());
        $order_id = input('param.order_id');
        $dot_lon =input('param.dot_lon');
        $dot_lat =input('param.dot_lat');
        $data['user_id'] = $user_id;
        $data['dot_lon'] = $dot_lon;
        $data['dot_lat'] = $dot_lat;
        $data['order_id'] = $order_id;
        $res = Db::table('up_order_dot')->insertGetId($data);
        if($res){
            return json(['status' => 200, 'msg' => "成功"]);
        }else{
            return json(['status' => 100, 'msg' => "失败"]);
        }
    }
    //判断司机是否能接单
    public function isJiedan(){
        extract(input());
        $where['order_dirver_user'] = $user_id;
        $where['order_status'] = array('<',7);
        $info = Db::table('up_order')->where($where)->find();
        if($info['order_id']!=''){
            return json(['status' => 100, 'msg' => "订单没有完成不能接单"]);
        }else{
            return json(['status' => 200, 'msg' => "可以接单"]);
        }
    }
    //返回订单状态
    public function orderDetail(){
        extract(input());
        $order_id = input('param.order_id');
        $res = Db::table('up_order')->where('order_id',$order_id)->field('order_status')->find();
        return json(['status' => 200, 'msg' => $res]);
    }
}